package com.fau.om.entity;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;
import org.json.simple.JSONAware;
import org.json.simple.JSONObject;

@Entity
@Table(name = "medical_resource")
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY, region = "atsoft_cache")
public class MedicalResource implements Serializable, JSONAware {

	private static final long serialVersionUID = -962894422616192007L;
	
	private String id;
	private String name;		// 资源名称
	private String url;			// 资源路径
	private String typeName;	// 所属系统
	private String pid;			// 所属版块父id
	private Integer isMenus;	// 是否是菜单  0:菜单  1:功能
	
	private Set<MedicalRole> roleSet;
	
	@Id
	@GenericGenerator(name = "generator", strategy = "uuid.hex")
	@GeneratedValue(generator = "generator")
	@Column(name = "ID", length = 32)
	public String getId() {
		return id;
	}
	
	public void setId(String id) {
		this.id = id;
	}
	
	@Column(name = "name", length = 50)
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	@Column(name = "url", length = 100)
	public String getUrl() {
		return url;
	}
	
	public void setUrl(String url) {
		this.url = url;
	}

	@Column(name = "type_name", length = 10)
	public String getTypeName() {
		return typeName;
	}

	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}

	@Column(name = "pid", length = 32)
	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	@Column(name = "is_menus", length = 2)
	public Integer getIsMenus() {
		return isMenus;
	}

	public void setIsMenus(Integer isMenus) {
		this.isMenus = isMenus;
	}

	@ManyToMany(mappedBy = "resourceSet", fetch = FetchType.EAGER)
	public Set<MedicalRole> getRoleSet() {
		return roleSet;
	}

	public void setRoleSet(Set<MedicalRole> roleSet) {
		this.roleSet = roleSet;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String toJSONString() {
		JSONObject obj = new JSONObject();
		obj.put("id", id);
		obj.put("text", name);
		if("1".equals(pid)) {
			obj.put("iconCls", "icon-addfolder");
		} else {
			obj.put("url", url);
			obj.put("pid", pid);
			obj.put("iconCls", "icon-node");
		}
		return obj.toJSONString();
	}

}
